package com.gator.desig_mode.state;


import java.util.Random;

/**
 * @author 何章怀晓 hezhanghuaixiao
 * @version V1.0
 * @desc 抽象行为
 */
public abstract class State {

  static int num = 1;

  abstract void deductMoney();//扣除积分

  abstract boolean reffle();//是否抽中奖品

  abstract void dispensePrize();//发放奖品


}

//不能抽奖状态,可扣积分，扣完后可抽奖
class NoRaffleState extends State {

  private RaffleActivte raffleActivte;

  public NoRaffleState(RaffleActivte raffleActivte) {
    this.raffleActivte = raffleActivte;
  }

  @Override
  void deductMoney() {
    if (num <= 0) {
      boolean reffle = reffle();
      if (!reffle) {
        return;
      }
    }
    System.out.println("扣除50积分，状态改为可抽奖");
    raffleActivte.setState(raffleActivte.getCanRaffleState());
  }


  @Override
  boolean reffle() {
    if (num <= 0) {

      System.out.println("或商品不足");
      raffleActivte.setState(raffleActivte.getDispenseOutState1());
      return false;
    } else {
      System.out.println("需先扣积分");
    }

    return false;
  }

  @Override
  void dispensePrize() {
    System.out.println("当前状态不能发放奖品");
  }
}

//可抽奖
class CanRaffleState extends State {

  private RaffleActivte raffleActivte;

  public CanRaffleState(RaffleActivte raffleActivte) {
    this.raffleActivte = raffleActivte;
  }

  @Override
  void deductMoney() {
    System.out.println("积分已扣除，可抽奖");
  }

  @Override
  boolean reffle() {
    System.out.println("抽奖中...");
    Random random = new Random();
    int i = random.nextInt(1);
    if (i == 0) {
      System.out.println("恭喜抽中...");
      //改变状态为发放奖品
      raffleActivte.setState(raffleActivte.getDispenseState());

      return true;

    } else {
      System.out.println("很遗憾未抽中...");
      //状态改为不可抽奖
      raffleActivte.setState(raffleActivte.getNoRaffleState());

      return false;
    }
  }

  @Override
  void dispensePrize() {
    System.out.println("没中奖，不能发放奖品...");
  }
}

//发放奖品状态
class DispenseState extends State {

  private RaffleActivte raffleActivte;

  public DispenseState(RaffleActivte raffleActivte) {
    this.raffleActivte = raffleActivte;
  }

  @Override
  void deductMoney() {
    System.out.println("不能扣积分...");
  }

  @Override
  boolean reffle() {
    System.out.println("发放奖品状态不能抽奖");
    return false;
  }

  @Override
  void dispensePrize() {

    System.out.println("发放奖品中");
    int i = num--;
    System.out.println("商品剩余:" + i);

    //状态置为不能抽奖
    raffleActivte.setState(raffleActivte.getNoRaffleState());


  }
}

//奖品领完，活动结束
class DispenseOutState extends State {

  private RaffleActivte raffleActivte;

  public DispenseOutState(RaffleActivte raffleActivte) {
    this.raffleActivte = raffleActivte;
  }


  @Override
  void deductMoney() {
    System.out.println("活动结束");
  }

  @Override
  boolean reffle() {
    return false;
  }

  @Override
  void dispensePrize() {
    System.out.println("活动结束");

  }
}
